<template>
    <div>
        <h2>盘点管理</h2>
        <el-divider></el-divider>
        <el-form :inline="true" :model="sardate" class="demo-form-inline demo-form-inline-left">
            <el-form-item label="盘点单号">
                <el-input v-model="sardate.checkCode" placeholder="盘点单号" style="width: 150px;"></el-input>
            </el-form-item>
            <el-form-item label="开始日期">
                <el-date-picker v-model="sardate.creatOrTime" type="date" placeholder="开始日期" style="width: 150px;">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期">
                <el-date-picker v-model="sardate.creatOrTimes" type="date" placeholder="结束日期" style="width: 150px;">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="产品名称">
                <el-select v-model="sardate.state" placeholder="仓库" clearable style="width:150px">
                    <el-option label="全部" :value="0">全部</el-option>
                    <el-option label="等待审核" :value="1">等待审核</el-option>
                    <el-option label="审核成功" :value="2">审核成功</el-option>
                    <el-option label="审核驳回" :value="3">审核驳回</el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="GetCheckDtoPes" icon="el-icon-search">查询</el-button>
                <el-button type="primary" class="el-icon-refresh-right" @click="reseted">重置</el-button>
                <el-button type="success" class="el-icon-plus" @click="Add">新增</el-button>
                <el-button type="success" class="el-icon-refresh" @click="reseted">刷新</el-button>
                <el-button type="danger" @click="getdels">批量删除</el-button>
            </el-form-item>
        </el-form>

        <!-- 列表 -->
        <template>
            <el-table :data="gerchecklist" border style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column prop="checkCode" label="盘点编号">
                </el-table-column>
                <el-table-column prop="checkType" label="盘点名称">
                </el-table-column>
                <el-table-column prop="state" label="盘点状态">
                    <template slot-scope="sc">
                        <span v-if="sc.row.state == 1">等待审核</span>
                        <span v-else-if="sc.row.state == 2">审核成功</span>
                        <span v-else>审核驳回</span>
                    </template>
                </el-table-column>
                <el-table-column prop="creatOr" label="制单人">

                </el-table-column>
                <el-table-column prop="creatOrTime" label="制单时间">
                    <template slot-scope="sc">
                        {{ sc.row.creatOrTime.substr(0, 10) }}
                    </template>
                </el-table-column>
                <el-table-column label="操作列">
                    <template slot-scope="sc">
                        <el-link type="primary" @click="look(sc.row.checkCode)">查看</el-link>
                        <el-link type="success" v-if="sc.row.state == 1" @click="handleClick(sc.row.checkCode)">审核</el-link>
                        <el-link type="success" v-if="sc.row.state == 1" @click="upd(sc.row.checkCode)">编辑</el-link>
                        <el-link type="danger" v-if="sc.row.state == 1" @click="del(sc.row.checkCode)">删除</el-link>
                    </template>
                </el-table-column>
            </el-table>
        </template>

        <!-- 查看 -->
        <el-dialog title="查看" :visible.sync="dialogTableVisible">

            <el-form :model="form" ref="ruleForm" label-width="100px" class="demo-ruleForm">

                <el-row :gutter="20">
                    <el-col :span="8" :offset="4">
                        <el-form-item label="盘点单号" prop="CheckCode">
                            <el-input v-model="form.CheckCode" readonly></el-input>
                        </el-form-item></el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item label="盘点类型" prop="CheckType">
                            <el-input v-model="form.CheckType" readonly></el-input>
                        </el-form-item></el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :span="8" :offset="4"><el-form-item label="制单人" prop="CreatOr">
                            <el-input v-model="form.CreatOr" readonly></el-input>
                        </el-form-item></el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item label="备注" prop="Remarks">
                            <el-input v-model="form.Remarks" readonly></el-input>
                        </el-form-item></el-col>
                </el-row>

            </el-form>

            <template>
                <el-table :data="productlist" border style="width: 100%">

                    <el-table-column prop="productName" label="产品名称">
                    </el-table-column>
                    <el-table-column prop="productCode" label="产品编码">
                    </el-table-column>
                    <el-table-column prop="specs" label="规格">
                    </el-table-column>
                    <el-table-column prop="batch" label="批次">

                    </el-table-column>
                    <el-table-column prop="location" label="库位">
                    </el-table-column>
                    <el-table-column prop="inventory" label="库存">
                    </el-table-column>
                    <el-table-column prop="checkNumber" label="盘点数">

                    </el-table-column>
                </el-table>
                <div style="text-align: right;">
                    <el-button type="primary" @click="dialogTableVisible = false">取 消</el-button>
                </div>
            </template>

        </el-dialog>

        <!-- 审核 -->
        <el-dialog title="审核" :visible.sync="dialogTableVisibles" width="80%">

            <el-form :model="form" ref="ruleForm" label-width="100px" class="demo-ruleForm">

                <el-row :gutter="20">
                    <el-col :span="8" :offset="4">
                        <el-form-item label="盘点单号" prop="CheckCode">
                            <el-input v-model="form.CheckCode" readonly></el-input>
                        </el-form-item></el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item label="盘点类型" prop="CheckType">
                            <el-input v-model="form.CheckType" readonly></el-input>
                        </el-form-item></el-col>
                </el-row>

                <el-row :gutter="20">
                    <el-col :span="8" :offset="4"><el-form-item label="制单人" prop="CreatOr">
                            <el-input v-model="form.CreatOr" readonly></el-input>
                        </el-form-item></el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item label="备注" prop="Remarks">
                            <el-input v-model="form.Remarks"></el-input>
                        </el-form-item></el-col>
                </el-row>
                <el-row :gutter="20">
                    <el-col :span="8" :offset="4"><el-form-item label="审批人" prop="reviewer">
                            <el-input v-model="form.reviewer"></el-input>
                        </el-form-item></el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item label="审批意见" prop="opinion">
                            <el-input v-model="form.opinion"></el-input>
                        </el-form-item></el-col>
                </el-row>

            </el-form>

            <template>
                <el-table :data="productlist" border style="width: 100%">

                    <el-table-column prop="productName" label="产品名称">
                    </el-table-column>
                    <el-table-column prop="productCode" label="产品编码">
                    </el-table-column>
                    <el-table-column prop="specs" label="规格">
                    </el-table-column>
                    <el-table-column prop="batch" label="批次">

                    </el-table-column>
                    <el-table-column prop="location" label="库位">
                    </el-table-column>
                    <el-table-column prop="inventory" label="库存">
                    </el-table-column>
                    <el-table-column prop="checkNumber" label="盘点数">

                    </el-table-column>

                </el-table>
            </template>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="Approval(2)">审批</el-button>
                <el-button type="primary" @click="overrule(3)">驳回</el-button>
                <el-button @click="dialogTableVisibles = false">取 消</el-button>
            </div>
        </el-dialog>


        <!-- 分页 -->
        <el-form :inline="true" class="btu">
            <el-form-item>
                共{{ page.totalCount }}条,共{{ page.pageCount }}页,当前{{ page.page }}/{{ page.page.pageCount }}页,每页显示{{ page.size
                }}条
            </el-form-item>
            <el-form-item>
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="page.page" :page-sizes="[5, 10, 15, 20]" :page-size="page.size"
                    layout="sizes,slot, prev, pager, next" :total="page.totalCount" prev-text="上一页" next-text="下一页">
                    <button @click="handleCurrentChange(1)" :disabled="page.page == 1">首页</button>
                </el-pagination>
            </el-form-item>
            <el-form-item>
                <el-pagination layout="slot, jumper" :total="page.totalCount">
                    <button @click="handleCurrentChange(page.pageCount)" :disabled="page.page == page.pageCount">尾页</button>
                </el-pagination>

            </el-form-item>
        </el-form>

    </div>
</template>
<script>
import { GetCheckDtoPes, DelCheck, DeleteBatchCheck, ReckCheck, UpdateCheckState } from '@/api/WarehouseServer/checkServer';
export default {
    //数据
    data() {
        return {
            sardate: {
                checkCode: "",
                creatOrTime: "",
                creatOrTimes: "",
                state: 0,
            },
            page: {
                page: 1,
                size: 5,
                totalCount: 0,
                pageCount: 0,
            },
            gerchecklist: [],
            multipleSelection: [],
            dialogTableVisible: false,

            form: {
                id: "",
                CheckCode: "",
                CheckType: "",
                CreatOr: "",
                Remarks: "",
                state: "",
                creatOrTime: "",
                reviewer: "",
                opinion: "",
            },
            productlist: [],

            //审批
            dialogTableVisibles: false,

        }
    },
    methods: {
        //审批弹出层
        handleClick(id) {
            this.dialogTableVisibles = true;
            this.ReckCheck(id);
        },
        //审批
        Approval(id) {
            this.UpdateCheckState(id);
        },
        //驳回
        overrule(id) {
            this.UpdateCheckState(id);
        },
        //审批方法
        UpdateCheckState(id) {
            if (this.form.reviewer.length == 0) {
                this.$message.success('审批人不能为空')
                return
            }

            var myparam = {
                checkCode: this.form.CheckCode,
                state: id,
                reviewer: this.form.reviewer,
                opinion: this.form.opinion,
            }
            UpdateCheckState(myparam).then((res) => {
                if (res.data.code == 0) {
                    this.$message({
                        message: "审批成功",
                        type: "success",
                    });
                    this.dialogTableVisibles = false;
                    this.page.page = 1;
                    this.GetCheckDtoPes();
                } else {
                    this.$message({
                        message: "审批失败",
                        type: "success",
                    });
                }
            })
        },
        //查看
        look(id) {
            console.log(id);
            this.dialogTableVisible = true;
            this.ReckCheck(id);
        },
        //查看
        ReckCheck(id) {
            //console.log(this.$route.query.id);
            var myparam = {
                checkCode: id,
            }
            ReckCheck(myparam).then((res) => {
                console.log(res.data);
                this.form = {
                    id: res.data.id,
                    CheckCode: res.data.checkCode,
                    CheckType: res.data.checkType,
                    CreatOr: res.data.creatOr,
                    Remarks: res.data.remarks,
                    state: res.data.state,
                    creatOrTime: res.data.creatOrTime,
                };
                this.form.reviewer = sessionStorage.getItem("encode");
                this.productlist = res.data.productManagementDto;
            });
        },
        //添加
        Add() {
            this.$router.push("/WarehouseManage/ChecksManage/CheckAdd");

        },
        //修改
        upd(id) {
            this.$router.push("/WarehouseManage/ChecksManage/CheckUpdate?id=" + id);
        },
        //分页
        handleCurrentChange(val) {
            this.page.page = val;
            this.GetCheckDtoPes();
        },
        handleSizeChange(val) {
            this.page.size = val;
            this.GetCheckDtoPes();
        },
        //重置
        reseted() {
            this.sardate = {
                checkCode: "",
                creatOrTime: "",
                creatOrTimes: "",
                state: 0,
            }
            this.GetCheckDtoPes();
        },

        //多选列表获取
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        //批量删除
        getdels() {
            if (this.multipleSelection.length == 0) {
                this.$message({
                    message: '请选择要删除的数据',
                    type: 'warning'
                });
                return
            }
            this.$confirm('此操作将永久批量删除该文件?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                DeleteBatchCheck(this.multipleSelection).then((res) => {
                    if (res.data.code == 0) {
                        this.$message({
                            message: "批量删除成功",
                            type: "success",
                        });
                        this.page.page = 1;
                        this.GetCheckDtoPes();
                    } else {
                        this.$message({
                            message: "批量删除失败",
                            type: "success",
                        });
                    }
                })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消批量删除'
                });
            });
        },
        //单位删除
        del(id) {
            var myparams = {
                checkCode: id,
            }
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                DelCheck(myparams).then((res) => {
                    if (res.data.code == 0) {
                        this.$message({
                            message: "删除成功",
                            type: "success",
                        });
                        this.page.page = 1;
                        this.GetCheckDtoPes();
                    } else {
                        this.$message({
                            message: "删除失败",
                            type: "success",
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        GetCheckDtoPes() {
            var myparams = {
                checkCode: this.sardate.checkCode,
                creatOrTime: this.sardate.creatOrTime,
                creatOrTimes: this.sardate.creatOrTimes,
                state: this.sardate.state,
                page: this.page.page,
                size: this.page.size,
            }
            GetCheckDtoPes(myparams).then((res => {
                console.log(res.data.data);
                this.gerchecklist = res.data.data;
                this.page.totalCount = res.data.totalCount;
                this.page.pageCount = res.data.pageCount;
            }))
        }
    },
    created() {


    },
    mounted() {
        this.GetCheckDtoPes();
        console.log(sessionStorage.getItem("encode"));
    },
}
</script>
<style>
.demo-form-inline-left {
    text-align: left;
}

.demo-form-inline-right {
    text-align: right;
}
</style>